//
//  CJEmoticonCell.swift
//  SinaWeibo
//
//  Created by 程建 on 16/10/8.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class CJEmoticonCell: UICollectionViewCell {

    var emoticons: [CJEmoticon]? {
       
        didSet{
            
            for btn in buttonArray {
                btn.isHidden = true
            }
            
            //一个萝卜对应一个坑
            for (index ,em) in emoticons!.enumerated() {
                
                //获取按钮
                let btn = buttonArray[index]
                btn.isHidden = false
                btn.emoticon = em
            }
            
        }
        
    }
    
    lazy var buttonArray:[CJEmoticonButtton] = [CJEmoticonButtton]()
    
    var indexPath: IndexPath? {
        
        didSet{
            
            testLabel.text = "第\(indexPath!.section)组,第\(indexPath!.item)行"
            
        }
    
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        contentView.addSubview(testLabel)
        
        testLabel.snp.makeConstraints { (make) in
            make.edges.equalTo(contentView)
        }
        
        //添加20个坑
        addChildButtons()
        
        //添加手势
        let longPre = UILongPressGestureRecognizer(target: self, action: #selector(longPress(ges:)))
        
        //添加到contentView上面
        contentView.addGestureRecognizer(longPre)
        
    }
    
    @objc private func longPress(ges:UILongPressGestureRecognizer) {
        
        //1. 根据手势触摸的点 --> 查找按钮  --> 模型数据  -> popoView需要模型数据
        let point = ges.location(in: contentView)
        
        //根据触摸点找到表情
        guard let btn = findEmoticonBtn(point: point) else {
            //移除popoView
            popoView.removeFromSuperview()
            return
        }
        
        //判断按钮是否是显示的,如果不显示就直接return
        if btn.isHidden == true {
            
            popoView.removeFromSuperview()
            return
        }
        
        
        switch ges.state {
        case .began,.changed:
            let window = UIApplication.shared.windows.last!
            //转换坐标 按钮需要将坐标转化到哪个View上面
            //转换的坐标填什么有规律: 转换自己的坐标的时候就是用bounds 如果转换父视图的坐标的就用frame
            let rect = btn.convert(btn.bounds, to: window)
            // btn.superview?.convert(btn.frame, to: window)
            //添加到contentView上
            popoView.center.x = rect.midX
            popoView.frame.origin.y = rect.maxY - popoView.bounds.height
            //给表情按钮设置模型
            popoView.emoticonButton.emoticon = btn.emoticon
            window.addSubview(popoView)
            popoView.show()
        default:
            popoView.removeFromSuperview()
        }
        
        
        
    }
    
   private func findEmoticonBtn(point:CGPoint) -> CJEmoticonButtton? {
        
        for btn in buttonArray {
            
            if btn.frame.contains(point) {
                return btn
            }
        }
        return nil
    }
    
    
    private func addChildButtons() {
    
        //添加20个坑(按钮)
        let buttonMargin:CGFloat = 30   //pageControl的显示范围
        let btnW = ScreenWidth / 7
        let btnH = (emoticonKeyboardHeight - emoticonToolBarHeight - buttonMargin) / 3
        for i in 0..<emticonPageCount {
            
            let btn = CJEmoticonButtton()
            
            //计算行和列
            let col = i % 7
            let row = i / 7
            //计算x,y
            let btnX = CGFloat(col)*btnW
            let btnY = CGFloat(row)*btnH
            btn.frame = CGRect(x: btnX, y: btnY, width: btnW, height: btnH)
            btn.backgroundColor = UIColor.blue
            //设置字体大小,用来显示emoji表情的
            btn.titleLabel?.font = UIFont.systemFont(ofSize: 32)
            contentView.addSubview(btn)
            buttonArray.append(btn)
            
            //监听按钮的点击事件
            btn.addTarget(self, action: #selector(emoticonBtnDidClick(btn:)), for: .touchUpInside)
            
        }
        //添加删除按钮
        let deleteBtn = UIButton()
        deleteBtn.frame = CGRect(x:ScreenWidth - btnW, y: 2*btnH, width: btnW, height: btnH)
        deleteBtn.setImage(#imageLiteral(resourceName: "compose_emotion_delete"), for: .normal)
        contentView.addSubview(deleteBtn)
        
    }
    
    @objc private func emoticonBtnDidClick(btn: CJEmoticonButtton) {
    
        //使用自定义UIButton,从按钮中获取一个模型属性
        //将表情模型存储到本地
        CJEmoticonTools.sharedEmoticonTools.saveRecentEmoticons(em: btn.emoticon!)
    }
    
    lazy var popoView:CJEmoticonPopoView = {
        let popoView = CJEmoticonPopoView.loadPopoView()
        
        return popoView
    }()
    
    
    lazy var testLabel:UILabel = UILabel(title: "", textColor: UIColor.black, fontSize: 25)
    
}
